//
//  TTotheAuthDataListController.m
//  TaTogether
//
//  Created by 九月 on 10/12/24.
//

#import "TTotheAuthDataListController.h"

@interface TTotheAuthDataListController ()<UITableViewDelegate , UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic ,strong) UIView *tableviewHeaderView;
@property (nonatomic ,strong) UITextField *searchTF;
@property (nonatomic ,assign) BOOL issearch; //是否搜索
@property (nonatomic ,strong) NSArray *searchResultArray;//搜索结果数组

@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *ttatheDataSource;

@end

@implementation TTotheAuthDataListController

- (UIView *)tableviewHeaderView
{
    if (!_tableviewHeaderView) {
        _tableviewHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,[[UIScreen mainScreen] bounds].size.height,56)];
        _tableviewHeaderView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1];
        
        
        UIView *ttathe = [[UIView alloc]initWithFrame:CGRectMake(12,8,[[UIScreen mainScreen] bounds].size.width-24,40)];
        ttathe.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
        ttathe.layer.cornerRadius = ttathe.height * 0.5;
        ttathe.layer.masksToBounds = YES;
        [_tableviewHeaderView addSubview:ttathe];
        
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(10,0,[[UIScreen mainScreen] bounds].size.width-44,40)];
        _searchTF.delegate = self;
        _searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchTF.returnKeyType =  UIReturnKeySearch;
        [_searchTF addTarget:self action:@selector(changeTextFile:) forControlEvents:UIControlEventEditingChanged];
        [ttathe addSubview:_searchTF];
        NSString *placeString;
        if (_certificationtype == certificationTypeCart) {
            if (_carString.length>0) {
                placeString = @"请输入车型";
            }else
            {
                placeString = @"请输入车辆品牌";
            }
        }
        else if (_certificationtype == certificationTypeDegree)
        {
            placeString = @"请输入学校关键字";
        }
        _searchTF.placeholder = placeString;
        _searchTF.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
//        [YUBKeepAppToolClass ControlSetTextfile:_searchTF PlaceColorTitle:placeString placeColor:UIColorFromString(@"#[UIColor colorWithRed:184/255.0 green:187/255.0 blue:190/255.0 alpha:1]") leftViewImageName:@"schoolSearch" leftViewFrame:CGRectMake(0, 0, 40, 30) tintColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1] fontSize:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular]];
    }
    return _tableviewHeaderView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (_certificationtype == certificationTypeCart) {
        if (_carString.length>0) {
            self.navigationItem.title = @"车型";
        }else
        {
            self.navigationItem.title = @"品牌";
        }
    }
    else if (_certificationtype == certificationTypeDegree)
    {
        self.navigationItem.title = @"毕业院校";
    }
    [self getListDataRequstiskeyword:_carString isfirst:YES];
    
    [self ttotheSetupUI];
}

- (void)ttotheSetupUI {
    self.tableView.tableHeaderView = self.tableviewHeaderView;
    self.tableView.rowHeight = 54.0;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];;
//    self.tableView.tableHeaderView = self.headerView;
    self.tableView.backgroundColor = UIColor.clearColor;
//    [self.tableView registerClass:[YOUfanTTWallDataCell class] forCellReuseIdentifier:YOUfanConstants.cunguTTwallDataCellID];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _issearch ? self.searchResultArray.count : self.ttatheDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"certificationListCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"certificationListCell"];
        UILabel *label = cell.textLabel;
        label.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        label.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        label.left = 12;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (_issearch) {
        if (self.searchResultArray.count > indexPath.row) {
            cell.textLabel.text = self.searchResultArray[indexPath.row];
        }
    }else
    {
        if (self.ttatheDataSource.count > indexPath.row) {
            cell.textLabel.text = self.ttatheDataSource[indexPath.row];
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_issearch) {
        if (self.searchResultArray.count > indexPath.row) {
            self.searchTF.text = self.searchResultArray[indexPath.row];
            if (_backBlock) {
                _backBlock(self.searchResultArray[indexPath.row]);
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }else
    {
        if (self.ttatheDataSource.count > indexPath.row) {
            self.searchTF.text = self.ttatheDataSource[indexPath.row];
            if (_backBlock) {
                _backBlock(self.ttatheDataSource[indexPath.row]);
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }
}
- (void)changeTextFile:(UITextField *)textField
{
    //这里搜索内容
    if (textField.text.length > 0) {
        _issearch = YES;
        NSString *textString = textField.text;
        if (_certificationtype == certificationTypeCart && _carString.length>0) {
            NSMutableArray *mutableArray = [NSMutableArray array];
            for (int i = 0; i < self.ttatheDataSource.count; i++) {
                NSString *content = self.ttatheDataSource[i];
                if ([content containsString:textString]) {
                    [mutableArray addObject:content];
                }
            }
            self.searchResultArray = mutableArray;
            [self.tableView reloadData];
        }else
        {
            [self getListDataRequstiskeyword:textString isfirst:NO];
        }
    }
    else
    {
        _issearch = NO;
        [self.tableView reloadData];
    }
    
}

////获取学校，车辆信息
- (void)getListDataRequstiskeyword:(NSString *)keyword isfirst:(BOOL)first
{
    NSString *requestUrl;
    if (_certificationtype == certificationTypeDegree) {
        requestUrl = @"/api/dictionary/full_college_list";
        
    }else if (_certificationtype == certificationTypeCart)
    {
        if (_carString.length>0) {
            requestUrl = @"/api/dictionary/full_car_module_list";
        }else
        {
            requestUrl = @"/api/dictionary/full_car_brand_list";
        }
    }
    NSMutableString *mutableStringUrl = [NSMutableString stringWithString:requestUrl];
    
    if (keyword.length>0) {
        [mutableStringUrl appendString:[NSString stringWithFormat:@"?keyword=%@",keyword]];
    }
    __weak typeof(self) fyweakSelf = self;

    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.estimatedRowHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = UIColor.whiteColor;
    }
    return _tableView;
}

@end

